package com.think.teaching.controller;

import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.Class;
import com.think.teaching.service.impl.ClassServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 班级 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Controller
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "班级管理接口")
public class ClassController {

	private final ClassServiceImpl classService;

	public ClassController(ClassServiceImpl classService) {
		this.classService = classService;
	}

	@ApiOperation("添加新的班级信息")
	@PostMapping("admin/class")
	public Result AddNewClassInfo(@RequestBody() Class data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			Class _class = classService.AddNewClass(data);
			if (_class != null) {
				return Result.success(_class);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("更新班级信息")
	@RequestMapping(value = "admin/class", method = RequestMethod.PUT)
	public Result UpdateClassInfo(@RequestBody() Class data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			Class _class = classService.UpdateClass(data);
			if (_class != null) {
				return Result.success(_class);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("删除班级信息")
	@RequestMapping(value = "admin/class/{id}", method = RequestMethod.DELETE)
	public Result DeleteClassInfo(@PathVariable("id") Integer id) {
		Class _class = classService.getById(id);
		if (_class == null) {
			return Result.failure(ResultCode.CLASS_NOT_EXIST);
		} else {
			if (classService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}

	@ApiOperation("获取班级信息详情")
	@RequestMapping(value = "admin/class/{id}", method = RequestMethod.GET)
	public Result GetClassInfo(@PathVariable("id") Integer id) {
		Class _class = classService.getById(id);
		if (_class == null) {
			return Result.failure(ResultCode.CLASS_NOT_EXIST);
		} else {

			return Result.success(_class);
		}
	}

	@ApiOperation("管理员修改班级信息状态")
	@RequestMapping(value = "admin/class/state", method = RequestMethod.PUT)
	public Result UpdateClassState(@RequestParam("id") Integer id, @RequestParam("state") Integer state) {
		Class _class = classService.getById(id);
		if (_class == null) {
			return Result.failure(ResultCode.CLASS_NOT_EXIST);
		} else {
			try {
				_class = classService.SetClassState(id, state);
				return Result.success(_class);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}

	@ApiOperation("用户搜索班级信息")
	@RequestMapping(value = "user/class/search", method = RequestMethod.GET)
	public Result UserSearchClass(@RequestParam("key") String key) {
		return Result.success(classService.SearchClassWithKey(key, true));
	}

	@ApiOperation("管理员搜索班级信息")
	@RequestMapping(value = "admin/class/search", method = RequestMethod.GET)
	public Result AdminSearchClass(@RequestParam("key") String key) {
		return Result.success(classService.SearchClassWithKey(key, true));
	}

	@ApiOperation("管理员搜索班级信息列表")
	@RequestMapping(value = "admin/classes/{page}/{count}", method = RequestMethod.GET)
	public Result AdminListClass(@PathVariable("page") Integer page, @PathVariable("count") Integer count,
			@RequestParam(value = "major", defaultValue = "-1") Integer major,
			@RequestParam(value = "college", defaultValue = "-1") Integer college,
			@RequestParam(value = "school", defaultValue = "-1") Integer school) {
		return Result.success(classService.ListClassInfo(page, count, major,college,school, false));
	}

	@ApiOperation("用户搜索班级信息列表")
	@RequestMapping(value = "user/classes/{page}/{count}", method = RequestMethod.GET)
	public Result UserListClass(@PathVariable("page") Integer page, @PathVariable("count") Integer count,
			@RequestParam(value = "major", defaultValue = "-1") Integer major,
			@RequestParam(value = "college", defaultValue = "-1") Integer college,
			@RequestParam(value = "school", defaultValue = "-1") Integer school) {
		return Result.success(classService.ListClassInfo(page, count, major,college,school, true));
	}
}
